package features.advance.leetcode.interview.offer.easy;


import java.util.Deque;
import java.util.LinkedList;
import java.util.Stack;

/**
 * 用两个栈来模拟一个队列，增加元素，删除元素（找不到返回-1）
 * @author LIN
 * @date 2021-05-06
 */
public class Offer09 {


    public static void main(String[] args) {
        Solution offer09 = new Solution() {
            private Stack<Integer> stack1 = new Stack<>();
            private Stack<Integer> stack2 = new Stack<>();


            @Override
            public void appendTail(int value) {
                stack1.push(value);
            }

            @Override
            public int deleteHead() {
                if(stack2.isEmpty()){
                    while(!stack1.isEmpty()){
                        stack2.push(stack1.pop());
                    }
                }
                if(!stack2.isEmpty()){
                    return stack2.pop();
                }else{
                    return -1;
                }
            }
        };
        System.out.println(offer09.deleteHead());
        offer09.appendTail(5);
        offer09.appendTail(2);
        System.out.println(offer09.deleteHead());
        System.out.println(offer09.deleteHead());
    }
    static class Solution{
        private Deque<Integer> stack1;
        private Deque<Integer> stack2;

        public Solution() {
            stack1 = new LinkedList<Integer>();
            stack2 = new LinkedList<Integer>();
        }

        public void appendTail(int value) {
            stack1.push(value);
        }

        public int deleteHead() {
            if(stack2.isEmpty()){
                while(!stack1.isEmpty()){
                    stack2.push(stack1.pop());
                }
            }

            if(stack2.isEmpty()){
                return -1;
            }else{
                return stack2.pop();
            }
        }
    }
}
